﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace PolePosition.ControlLibrary.Common
{
    public class TransparentPictureBox : System.Windows.Forms.PictureBox
    {
        public TransparentPictureBox()
        {
            this.BackColor = Color.Transparent;
        }

        //protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
        //{
        //    return;
        //    SolidBrush _SolidBrush;
        //    _SolidBrush = new SolidBrush(Parent.BackColor);
        //    e.Graphics.FillRectangle(_SolidBrush, this.ClientRectangle);
        //}

        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);
            //Graphics g = e.Graphics;
            //Font font = new Font(FontFamily.GenericMonospace, 2, FontStyle.Bold);
            //SolidBrush brush = new SolidBrush(Color.Transparent);
            //g.DrawString(this.Text, font, brush, 1,1);
            //g.Dispose();


            if (this.Image != null)
            {
                Bitmap bmp = new Bitmap(this.Image);
                System.Drawing.Imaging.ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes();
                attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0));
                Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attr);
            }

            if (this.Image == null)
                return;

            System.Drawing.Imaging.ImageAttributes _ImageAttributes;
            _ImageAttributes = new System.Drawing.Imaging.ImageAttributes();

            Color _Transparent;
            _Transparent = Color.FromArgb(255, 0, 255);

            _ImageAttributes.SetColorKey(_Transparent, _Transparent);
            e.Graphics.DrawImage(this.Image, this.ClientRectangle, 0, 0,
                this.Image.Width, this.Image.Height, GraphicsUnit.Pixel, _ImageAttributes);
        }
    }
}